package com.sczmgk.track.activity;

import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sczmgk.track.MyApp;
import com.sczmgk.track.R;
import com.sczmgk.track.base.CheckPermissionBaseActivity;
import com.sczmgk.track.constant.Constant;
import com.sczmgk.track.dialog.DialogFactory;
import com.sczmgk.track.dialog.MessageDialog;
import com.sczmgk.track.httpUtils.MyHttp;
import com.sczmgk.track.test.MyCallback;
import com.sczmgk.track.utils.CacheUtils;
import com.sczmgk.track.utils.CommonUtils;
import com.sczmgk.track.utils.MyToast;
import com.sczmgk.track.utils.Tools;
import com.sczmgk.track.utils.UIUtils;
import com.sczmgk.track.view.CustomDeleteInfoView;

import java.util.HashMap;
import java.util.Map;

import static com.sczmgk.track.constant.Constant.managerPhone;

/**
 * Created by Administrator on 2017/10/9.
 */
public class ChangePwdActivity extends CheckPermissionBaseActivity {

    private static final int REFRESH_UI = 0;
    private CustomDeleteInfoView mOldPwd;
    private CustomDeleteInfoView mNewPwd;
    private CustomDeleteInfoView mConfirmPwd;
    private Button mBtnSubmit;
    private MessageDialog userMessage;
    private TextView mTvForgetPwd;
    private MessageDialog callPhoneDialg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        CommonUtils.addActivity(this);
        init();
    }

    private void init() {
        setTitle(R.string.left_change_pwd);
        initView();
        initData();
        setListener();
    }

    private void initView() {

        //保存修改
        mBtnSubmit = (Button) findViewById(R.id.btn_change_pwd_submit);

        //确认密码
        mConfirmPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_pwd_confirm);

        //新密码
        mNewPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_new_pwd);

        //旧密码
        mOldPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_chang_old_pwd);

        //忘记密码
        mTvForgetPwd = (TextView) findViewById(R.id.tv_change_pwd_forget_pwd);

        //设置单行
        mOldPwd.setSingleLine();
        mNewPwd.setSingleLine();
        mConfirmPwd.setSingleLine();

        //设置输入类型
        mOldPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mNewPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mConfirmPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void initData() {

    }

    private void setListener() {
        mBtnSubmit.setOnClickListener(new MyOnClickListener());

        //忘记密码
        mTvForgetPwd.setOnClickListener(new MyOnClickListener());
    }

    @Override
    public void onSelectedPhonePermission(String phoneNumber) {
        CommonUtils.callPhone(ChangePwdActivity.this ,phoneNumber);
    }

    @Override
    public void onSelectedStroage(int position) {

    }

    @Override
    public void onSelectCamera(int position) {

    }


    private class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_change_pwd_submit:

                    String oldPwd = mOldPwd.getText();
                    String newPwd = mNewPwd.getText();
                    String confirmPwd = mConfirmPwd.getText();

                    if (Tools.isEmpty(oldPwd)) {
                        MyToast.show(ChangePwdActivity.this, "请输入原密码");
                        return;
                    }

                    if (Tools.isEmpty(newPwd)) {
                        MyToast.show(ChangePwdActivity.this, "请输入新密码");
                        return;
                    }

                    if (Tools.isEmpty(confirmPwd)) {
                        MyToast.show(ChangePwdActivity.this, "请再次确认密码");
                        return;
                    }

                    if (oldPwd.equals(newPwd)) {
                        MyToast.show(ChangePwdActivity.this, "原密码与新密码相同");
                        return;
                    }

                    if (!newPwd.equals(confirmPwd)) {
                        MyToast.show(ChangePwdActivity.this, "两次输入密码不一致");
                        return;
                    }

                    // TODO 暂时不做校验  检查密码是否符合规范
//                    String correctToast = Tools.isCorrectPWD(newPwd, "");
//                    if (!correctToast.equals("true")) {
//                        MyToast.show(ChangePwdActivity.this, correctToast);
//                        return;
//                    }

                    //提交数据到服务器
                    submitData(oldPwd, confirmPwd);
                    break;

                case R.id.tv_change_pwd_forget_pwd :
                    //忘记密码
                    if (callPhoneDialg == null) {
                        showCallPhoneDialog();
                    } else if (!callPhoneDialg.isShowing()) {
                        showCallPhoneDialog();
                    }
                    break;
            }
        }
    }

    private void showCallPhoneDialog() {
        callPhoneDialg = DialogFactory.chooseDialog(ChangePwdActivity.this, getResources().getString(R.string.call_phone_des) + managerPhone, getResources().getString(R.string.sure), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkPhone(managerPhone);
            }
        });
    }

    /**
     * 提交数据
     */
    private void submitData(String oldPwd, String confirmPwd) {
        showLoadingDiaLog();

        Map<String, Object> maps = new HashMap<>();
        maps.put("userid", CacheUtils.getLoginUserid(ChangePwdActivity.this));
        maps.put("oldpwd", oldPwd);
        maps.put("newpwd", confirmPwd);

        MyHttp.requestDataT(ChangePwdActivity.this, Constant.CHANGE_PWD_URL, MyHttp.POST, null, maps, null, false, null, new MyCallback() {

            @Override
            public void onSuccess(Object o) {
                dismissLoadingDiaLog();
                mHandler.sendEmptyMessage(REFRESH_UI);
            }

            @Override
            public void onUnknownError() {
                dismissLoadingDiaLog();
            }

            @Override
            public void onError(String msg) {
                dismissLoadingDiaLog();
                showErrorMsg(msg);
            }

            @Override
            public void onSuccess() {
                dismissLoadingDiaLog();
                mHandler.sendEmptyMessage(REFRESH_UI);
            }
        });
    }


    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case REFRESH_UI:
                if (userMessage == null) {
                    showSuccess();
                }else if (!userMessage.isShowing()){
                    showSuccess();
                }
                break;
        }
    }

    private void showSuccess() {
        userMessage = DialogFactory.warningDialog(ChangePwdActivity.this, getResources().getString(R.string.change_pwd_succeess), R.string.login_again, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CacheUtils.cacheLoginStatus(ChangePwdActivity.this,false);
                CacheUtils.setLoginUserid(ChangePwdActivity.this,-1);
                MyApp.loginUserUid = -1;
                MyApp.loginUserName = "userName";
                UIUtils.startActivity(ChangePwdActivity.this,LoginActivity.class);
                CommonUtils.finishActivity();
            }
        });
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }
}
